<article class="module width_full">
    <header><h3 class="tabs_involved">DANH MỤC SÁCH</h3>
        <ul class="tabs">
            <li><a href="#tab1">Tất cả</a></li>
        </ul>
    </header>
    <div class="tab_container"  style="overflow-y:scroll; height:200px">
        <div id="tab1" class="tab_content">
            <table class="tablesorter" cellspacing="0" > 
                <thead> 
                    <tr> 
                        <th>STT</th> 
                        <th>Mã loại</th> 
                        <th>Tên sách </th> 
                        <th>Hành động</th> 
                    </tr> 
                </thead> 
                <tbody id="category"> 
                    <?php
                    $i = 0;

                    foreach ($sachs as $value) {

                        $i+=1;
                        echo "<tr id='row" . $value->MATP . "'>";
                        echo '<td>' . $i . '</td>';
                        echo '<td>' . $value->MATP . '</td>';
                        echo '<td class="td_tentp">' . $value->TENTP . '</td>';
                        echo "<td><a class='edit-category' href='" . $value->MATP . "'><input type='image' src='" . base_url() . "public/admin/images/icn_edit.png' title='Chỉnh sửa phân loại hoa'></a>
					<a class='delete-cate' href='" . $value->MATP . "'><input type='image' src='" . base_url() . "public/admin/images/icn_trash.png'title='Xóa phân loại hoa'></a>
					<a class='add-cate'><input type='image' src='" . base_url() . "public/admin/images/icn_add_user.png' title='Thêm phân loại hoa mới'></a></td>";
                        echo '</tr>';
                    }
                    ?>
                </tbody> 
            </table>
        </div><!-- end of #tab1 -->


    </div><!-- end of .tab_container -->

</article><!-- end of content manager article -->	

<!-- Modal add -->
<div class="modal fade" id="myModal-add" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">Thêm mới sách</h4>
            </div>
            <div class="modal-body">
                <form id="form-add"  method="POST" class="form-horizontal" role="form" enctype="multipart/form-data">
                    <div class="form-group col-md-9">
                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-2 control-label">Tên sách</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control tennxb"  id="tentp-add" name="tennxb" placeholder="Tên sách">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-2 control-label">Giá gốc</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control tennxb"  id="giagoc-add" name="giagoc" placeholder="Giá gốc">
                            </div>
                            <label for="inputEmail3" class="col-sm-2 control-label">Giá bán</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control tennxb"  id="giaban-add" name="giaban" placeholder="Giá bán">
                            </div>                       
                        </div> 
                        <div class="form-group">                        
                            <label for="inputEmail3" class="col-sm-2 control-label">Số trang</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control tennxb"  id="sotrang-add" name="sotrang" placeholder="Số trang">
                            </div>
                            <label for="inputEmail3" class="col-sm-2 control-label">Năm XB</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control tennxb"  id="namxuatban-add" name="namxuatban" placeholder="Năm xuất bản">
                            </div>                        
                        </div> 
                    </div>
                    <div class="form-group col-md-3">                        
                        <!--                        <div class="col-md-1"></div>   -->
                        <div class="col-md-11">
                            <img id="img-add" src=""  class="img-thumbnail img-responsive" style="width: 140px;height: 140px;" />
                            <input type="file" class="form-control tennxb"  id="hinhanh-add" name="hinhanh">                        
                        </div>   

                    </div>
                    <div style="clear: both;"><hr></div>


                    <div class="form-group">
                        <label for="inputEmail3" class="col-sm-2 control-label"> Nhà xuất bản</label>
                        <div class="col-sm-4">
                            <select id="manxb-add" name="manxb" data-btn-class="btn-success" class="col-md-12 btn btn-inverse" >
                                <?php
                                foreach ($nxbs as $value) {
                                    ?>
                                    <option value="<?php echo $value->MANXB ?>"><?php echo $value->TENNXB ?></option>
                                    <?php
                                }
                                ?>
                            </select>

                        </div>
                        <label for="inputEmail3" class="col-sm-2 control-label">Tác giả</label>
                        <div class="col-sm-4">
                            <select id="matg-add" name="matg" data-btn-class="btn-success" class="col-md-12 btn btn-inverse" >
                                <?php
                                foreach ($tacgias as $value) {
                                    ?>
                                    <option value="<?php echo $value->MATG ?>"><?php echo $value->TENTG ?></option>
                                    <?php
                                }
                                ?>
                            </select>

                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputEmail3" class="col-sm-2 control-label">CAPTION</label>
                        <div class="col-sm-4"></div>
                        <label for="inputEmail3" class="col-sm-2 control-label">Thể loại</label>
                        <div class="col-sm-4">
                            <select id="matl-add" name="matl" data-btn-class="btn-success" class="col-md-12 btn btn-inverse" >
                                <?php
                                foreach ($theloais as $value) {
                                    ?>
                                    <option value="<?php echo $value->MATL ?>"><?php echo $value->TENTL ?></option>
                                    <?php
                                }
                                ?>
                            </select>

                        </div>                       
                    </div>                    

                    <div class="form-group">
                        <div class=" col-sm-12">
                            <?php echo form_textarea(array('name' => 'caption', 'id' => 'caption-add', 'class' => "ckeditor caption", 'placeholder' => 'Thông tin về nhà xuất bản')); ?>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button form="form-add" form="form-add" type="button" id="btn-add" class="btn btn-primary">Lưu</button>
            </div>
        </div>
    </div>
</div>
<!-- End Modal add -->
<!--Begin Modal edit -->

<div class="modal fade" id="myModal-edit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">Thêm mới sách</h4>
            </div>
            <div class="modal-body">
                <form id="form-edit"  method="POST" class="form-horizontal" role="form" enctype="multipart/form-data">
                    <div class="form-group col-md-9">
                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-2 control-label">Tên sách</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control tennxb"  id="tentp-edit" name="tennxb" placeholder="Tên sách">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-2 control-label">Giá gốc</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control tennxb"  id="giagoc-edit" name="giagoc" placeholder="Giá gốc">
                            </div>
                            <label for="inputEmail3" class="col-sm-2 control-label">Giá bán</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control tennxb"  id="giaban-edit" name="giaban" placeholder="Giá bán">
                            </div>                       
                        </div> 
                        <div class="form-group">                        
                            <label for="inputEmail3" class="col-sm-2 control-label">Số trang</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control tennxb"  id="sotrang-edit" name="sotrang" placeholder="Số trang">
                            </div>
                            <label for="inputEmail3" class="col-sm-2 control-label">Năm XB</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control tennxb"  id="namxuatban-edit" name="namxuatban" placeholder="Năm xuất bản">
                            </div>                        
                        </div> 
                    </div>
                    <div class="form-group col-md-3">                        
                        <!--                        <div class="col-md-1"></div>   -->
                        <div class="col-md-11">
                            <img id="img-edit" src=""  class="img-thumbnail img-responsive" style="width: 140px;height: 140px;" />
                            <input type="file" class="form-control tennxb"  id="hinhanh-edit" name="hinhanh">                        
                        </div>   

                    </div>
                    <div style="clear: both;"><hr></div>


                    <div class="form-group">
                        <label for="inputEmail3" class="col-sm-2 control-label"> Nhà xuất bản</label>
                        <div class="col-sm-4">
                            <select id="manxb-edit" name="manxb" data-btn-class="btn-success" class="col-md-12 btn btn-inverse" >
                                <?php
                                foreach ($nxbs as $value) {
                                    ?>
                                    <option value="<?php echo $value->MANXB ?>"><?php echo $value->TENNXB ?></option>
                                    <?php
                                }
                                ?>
                            </select>

                        </div>
                        <label for="inputEmail3" class="col-sm-2 control-label">Tác giả</label>
                        <div class="col-sm-4">
                            <select id="matg-edit" name="matg" data-btn-class="btn-success" class="col-md-12 btn btn-inverse" >
                                <?php
                                foreach ($tacgias as $value) {
                                    ?>
                                    <option value="<?php echo $value->MATG ?>"><?php echo $value->TENTG ?></option>
                                    <?php
                                }
                                ?>
                            </select>

                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputEmail3" class="col-sm-2 control-label">CAPTION</label>
                        <div class="col-sm-4"></div>
                        <label for="inputEmail3" class="col-sm-2 control-label">Thể loại</label>
                        <div class="col-sm-4">
                            <select id="matl-edit" name="matl" data-btn-class="btn-success" class="col-md-12 btn btn-inverse" >
                                <?php
                                foreach ($theloais as $value) {
                                    ?>
                                    <option value="<?php echo $value->MATL ?>"><?php echo $value->TENTL ?></option>
                                    <?php
                                }
                                ?>
                            </select>

                        </div>                       
                    </div>                    

                    <div class="form-group">
                        <div class=" col-sm-12">
                            <?php echo form_textarea(array('name' => 'caption', 'id' => 'caption-edit', 'class' => "ckeditor caption", 'placeholder' => 'Thông tin về nhà xuất bản')); ?>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button form="form-edit" form="form-edit" type="button" id="btn-edit" class="btn btn-primary">Lưu</button>
            </div>
        </div>
    </div>
</div>
<!-- End Modal edit -->
<div id="son"></div>
<script>
    $(document).ready(function() {
        $('select').btComboBox('value');
        $('.edit-category').click(function(e) {
            e.preventDefault();
            var matp = $(this).attr("href");
            globalMatp = matp;
            $.ajax({
                type: "POST",
                url: globalurl + 'index.php/admin/getSach',
                data: {
                    matp: matp
                },
                success: function(data) {

                    $("#tentp-edit").val(data.tentp);
                    $("#namxuatban-edit").val(data.namxb);
                    $("#giaban-edit").val(data.giaban);
                    $("#giagoc-edit").val(data.giagoc);
                    $("#sotrang-edit").val(data.sotrang);
                    $("#img-edit").attr("src", data.hinhanh);
                    $("#manxb-edit").val(data.manxb);
                    $("#matg-edit").val(data.matg);
                    $("#matl-edit").val(data.matl);
                    $("#myModal-edit").modal();
                    CKEDITOR.instances["caption-edit"].setData(data.caption);
                    $("#hinhanh-edit").text(data.hinhanh);
                }, dataType: 'json'
            });
        });
        $("#btn-edit").click(function(e) {
            e.preventDefault();
            var tentp = $("#tentp-edit").val();
            var hinhanh = document.getElementById("hinhanh-edit").files[0];
            var matl = $("#matl-edit").val();
            var matg = $("#matg-edit").val();
            var namxuatban = $("#namxuatban-edit").val();
            var manxb = $("#manxb-edit").val();
            var sotrang = $("#sotrang-edit").val();
            var giagoc = $("#giagoc-edit").val();
            var giaban = $("#giaban-edit").val();

            var caption = CKEDITOR.instances['caption-edit'].getData();
            var fd = new FormData();
            fd.append('matp', globalMatp);
            fd.append('file', hinhanh);
            fd.append('tentp', tentp);
            fd.append('matl', matl);
            fd.append('matg', matg);
            fd.append('namxuatban', namxuatban);
            fd.append('sotrang', sotrang);
            fd.append('caption', caption);
            fd.append('giaban', giaban);
            fd.append('giagoc', giagoc);
            fd.append('manxb', manxb);
            $.ajax({
                type: "POST",
                processData: false,
                contentType: false,
                url: globalurl + 'index.php/admin/sach_upd',
                data: fd,
                success: function(data) {
                    $("#img-edit").attr('src', data.hinhanh);
                    $("#row" + globalMatp).find(".td_tentp").text(data.tentp);
                    // $("#myModal-add").modal('hide');
                }, error: function(xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    alert(thrownError);
                }, dataType: 'json'
            });

        });
        var globalMatp;
       
        $('.delete-cate').click(function(e) {
            e.preventDefault();
            var matp = $(this).attr("href");
            if (confirm('Bạn có muốn xóa sách này không?')) {
                $.ajax({
                    type: "POST",
                    url: globalurl + 'index.php/admin/sach_del',
                    data: {
                        matp: matp
                    },
                    success: function(data) {
                        $('#row' + matp).hide();
                    }
                });
            }

        });
        $('.add-cate').click(function(e) {
            e.preventDefault();
            $("#myModal-add").modal();
        });
        $("#btn-add").click(function(e) {
            e.preventDefault();
            var tentp = $("#tentp-add").val();
            var hinhanh = document.getElementById("hinhanh-add").files[0];            
            var matl = $("#matl-add").val();
            var matg = $("#matg-add").val();
            var namxuatban = $("#namxuatban-add").val();
            var manxb = $("#manxb-add").val();
            var sotrang = $("#sotrang-add").val();
            var giagoc = $("#giagoc-add").val();
            var giaban = $("#giaban-add").val();

            var caption = CKEDITOR.instances['caption-add'].getData();
            var fd = new FormData();
            fd.append('file', hinhanh);
            fd.append('tentp', tentp);
            fd.append('matl', matl);
            fd.append('matg', matg);
            fd.append('namxuatban', namxuatban);
            fd.append('sotrang', sotrang);
            fd.append('caption', caption);
            fd.append('giaban', giaban);
            fd.append('giagoc', giagoc);
            fd.append('manxb', manxb);
            $.ajax({
                type: "POST",
                processData: false,
                contentType: false,
                url: globalurl + 'index.php/admin/sach_add',
                data: fd,
                success: function(data) {
                    //alert(data);
                  // $("#son").text(data.matp);
                  if(data.matp==true)$("#myModal-add").modal('hide');
                    $("#img-add").attr('src', globalurl + "index.php/product/GetImage/" + data.matp);
                    // $("#myModal-add").modal('hide');
                }, error: function(xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    alert(thrownError);
                }///, dataType: 'json'
            });
        });
////$("#btn-add").click(function (e){
////   
////});
        $('#btn-add').submit(function(e) {
            e.preventDefault();
            alert('x');
            var name_cate = $('#catename').val();
            $.ajax({
                type: "POST",
                url: globalurl + 'index.php/admin/addnewcate',
                data: {
                    catename: name_cate
                },
                success: function(data) {
                    $('#category').append(data);
                    $('#add_form').toggle("slow");
                }
            });
        });
    });

</script>
